
#include "WinExpection.h"

#include "windows.h"

using namespace sd::tools::win;




sd::tools::ErrroCode  WinImplException::getLastError()
{
	return GetLastError();
}



std::string  sd::tools::unicodeToAnsi(const std::wstring& str)
{
		char *ansistr = new char[str.size()+1];

		::WideCharToMultiByte(CP_ACP, 
								0, 
								str.c_str(), 
								-1, 
								ansistr, 
								str.size(), 
								NULL, 
								NULL);

		std::string result = ansistr;

		delete[] ansistr;

		return result;
}


std::wstring sd::tools::ansiToUnicode(const std::string& str)
{
	wchar_t* buf = new wchar_t[str.size()+1];
	MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.size(), buf, str.size());
	std::wstring result = buf;
	delete[] buf;
	return result;
}

std::wstring sd::tools::getWinErrorAsString( ErrroCode error )
{
    LPWSTR lpMsgBuf;

    FormatMessageW (
        FORMAT_MESSAGE_ALLOCATE_BUFFER | 
        FORMAT_MESSAGE_FROM_SYSTEM |
        FORMAT_MESSAGE_IGNORE_INSERTS,
        NULL,
        error,
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
        (LPWSTR) &lpMsgBuf,
        0, NULL );

	return  lpMsgBuf;
}

